//
//  MainTabBarView.swift
//  Weibo
//
//  Created by gdarkness on 16/3/20.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

let TabBarItemCount = 5

class MainTabBarView: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算itme 的宽度
        let width  = frame.width / CGFloat(TabBarItemCount)
        //计算出最左边的这个frame，后面使用CGRectOffser
        let originFrame = CGRect(x: 0, y: 0, width: width, height: frame.height)
        
        var index = 0
        for view in subviews{
            let cls = NSClassFromString("UITabBarButton")
            //判断是否UITabBarButton
            if view.isKindOfClass(cls!){
                view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: frame.height)
                index += index == 1 ? 2 : 1
            }
        }
        //设置加好按钮frame
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    //MARK : ---- lazy login
    private lazy var composeButton : UIButton = {
        let  button  = UIButton()
        //设置
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
       
        //添加到TabBar上面
        self.addSubview(button)
        
        return button
    }()

}
